//
//  RoomInfoLogic.m
//  QRMaster
//
//  Created by myApple on 15/12/22.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "RoomInfoLogic.h"
#import "OperationDataBaseModel.h"

@interface RoomInfoLogic()
{
    OperationDataBaseModel * operation;
}

@end

@implementation RoomInfoLogic

#pragma mark ------------------插入房间信息
-(void)insertRoomInfo:(RoomsModel *)model withFloorId:(NSString *)floorId withCommunityId:(NSString *)communityId
{
    NSMutableDictionary * staffDict = [self modelConvertDict:model];
    [staffDict setValue:floorId forKey:@"floorid"];
    [staffDict setValue:communityId forKey:@"communityentity_id"];
    operation = [[OperationDataBaseModel alloc]init];
    [operation insertDataTable:@"roomsInfo" withDict:staffDict];
}


#pragma mark ------------------查询房间信息
-(void)queryRoomInfo:(NSString *)floorId block:(roomDataBlock)roomArray
{
    __block NSMutableArray * roomMutableArray = [[NSMutableArray alloc]init];
    NSString * condition = [NSString stringWithFormat:@"where %@=%@",@"floorid",floorId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"roomsInfo" withCondition:condition block:^(FMResultSet *set) {
        while ([set next]) {
            RoomsModel * model = [[RoomsModel alloc]init];
            model.roomId = [set stringForColumn:@"roomid"];
            model.roomName = [set stringForColumn:@"roomname"];
            model.roomNo = [set stringForColumn:@"roomno"];
            model.occupation = [set stringForColumn:@"occupation"];
            model.lockNo = [set stringForColumn:@"lockno"];
            model.isSelect = [set stringForColumn:@"isSelected"];
            [roomMutableArray addObject:model];
        }
        if (roomArray) {
            roomArray(roomMutableArray);
        }
    }];
}

-(void)queryRoomName:(NSString *)roomId block:(myResultRoomName)roomName
{
    __block NSString * roomNameStr=@"";
    
    NSString * condition = [NSString stringWithFormat:@"where %@=%@",@"roomId",roomId];
     operation = [[OperationDataBaseModel alloc]init];
    [operation queryDataFromTable:@"roomsInfo" withCondition:condition block:^(FMResultSet *set) {
        while ([set next]) {
            roomNameStr = [set objectForColumnName:@"roomname"];
        }
        if (roomName) {
            roomName(roomNameStr);
        }
    }];

}

-(void)deleteRoomWithCommunityId:(NSString *)communityId
{
//    NSString * condition = [NSString stringWithFormat:@"where roomId=%@ and communityentity_id = %@",roomId];
    operation = [[OperationDataBaseModel alloc]init];
    [operation deleteBulidingFromTableByCommunityId:@"roomsInfo" communityId:communityId];
}


/**
 *  模型转化为字典
 *
 *  @param model 房间模型
 *
 *  @return 可变字典
 */
-(NSMutableDictionary *)modelConvertDict:(RoomsModel *)model
{
    NSMutableDictionary * staffDict = [[NSMutableDictionary alloc]init];
    [staffDict setValue:model.roomId forKey:@"roomid"];
    [staffDict setValue:model.isSelect forKey:@"isSelected"];
    [staffDict setValue:model.lockNo forKey:@"lockno"];
    [staffDict setValue:model.roomName forKey:@"roomname"];
    [staffDict setValue:model.roomNo forKey:@"roomno"];
    [staffDict setValue:model.occupation forKey:@"occupation"];
    return staffDict;
}

@end
